﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using PerformanceMonitor.Charts;

namespace PerformanceMonitor
{
    public partial class Graph : System.Web.UI.UserControl
    {
        public Graph()
        {
            Lines = new List<ChartLine>();
        }

        public string Title { get; set; }

        public string SubTitle { get; set; }

        public string YAxisName { get; set; }

        public DateTime StartDateTime { get; set; }

        public TimeSpan PointInterval { get; set; }

        public IList<ChartLine> Lines { get; private set; }

        protected string StartDateTimeRendered { get; set; }

        protected string LinesRendered { get; set; }

        protected override void OnPreRender(EventArgs e)
        {
            StartDateTimeRendered = "Date.UTC(" + StartDateTime.ToString("yyyy, MM, dd, hh, mm, ss") + ")";

            var  builder = new StringBuilder() ;

            builder.Append("series: [");

            var isFirst = true;

            foreach (var line in Lines)
            {
                if (!isFirst)
                {
                    builder.Append(",");
                }
                else
                {
                    isFirst = false;
                }

                builder.AppendFormat(
                    @"{{
					type: 'spline',
					name: '{0}',
					pointInterval: {1},
					pointStart: {2},
					data: [{3}]
				    }}", 
                   line.Name, 
                   PointInterval.Ticks / 10000,
                   StartDateTimeRendered, 
                   string.Join(",", line.Data.Select(l => l.ToString(CultureInfo.InvariantCulture)).ToArray()));
            }

            builder.Append("]");

            LinesRendered = builder.ToString();
        }
    }
}